#include "capture.h"
static int pcap_enable = 0;
static char pcap_file[256] = "./raw.pcap";
static int pcap_count = 0;
static int pcap_file_size = 0;
pcap_output_t* pout = NULL;

int pcap_output_init(int argc, char **argv){
    OPT_T opts[] = {
        {"--pcap-enable", &pcap_enable, OPT_NONE, "enable output to pcap file."},
        {"--pcap-file", pcap_file, OPT_STRING, "output pcap file."},
        {"--pcap-count", &pcap_count, OPT_INT, "capture packets max count."},
        {"--pcap-size", &pcap_file_size, OPT_INT, "pcap file max size."},
    };
    opt_parse(opts, sizeof(opts), argc, argv);

    if(pcap_enable == 0){
        return 0;
    }

    pout = pcap_output_new(pcap_file, pcap_file_size, pcap_count);
    return 0;
}

int pcap_output_hook(Packet_t* packet)
{
    int ret;
    if(!pout){
        return 0;
    }
    ret = pcap_output(pout, packet);
    switch(ret){
        case PCAP_OUTPUT_ERR_MAX_SIZE:
            log_warn("pcap reach max file size %dKB", pcap_file_size);
            break;
        case PCAP_OUTPUT_ERR_MAX_COUNT:
            log_warn("pcap reach max packets count %d", pcap_count);
            break;
        case PCAP_OUTPUT_ERR_NONE:
            break;
        default:
            break;
    }
    return 0;
}